﻿using System;
using System.Web.Mvc;
using SrBBSPro.Entities;
using SrBBSPro.Services;
using SrBBSPro.Models;
using SrBBSPro.BaseClasses;
using System.Collections.Generic;

namespace SrBBSPro.Controllers
{
    public class ProjectController : BaseController
    {
        public ActionResult Index()
            => View();

        public ActionResult DataList()
        {
            var li = new ProjectService()
                .GetProjectList(Request["Depart"], Request["name"], out int count, Convert.ToInt32(Request["page"]));
            return JsonResultForLay<Project, long>(li, count, false);
        }


        [HttpPost]
        public ActionResult ProjectNames(string kind)
            => JsonResultOption(new ProjectService().GetProjectNames(kind));


        public ActionResult Add()
            => View();

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Add(ProjectModel model)
        {
            var response = new ProjectService().InsertOrUpdate(model);
            return JsonResultForFly(response.IsSuccess ? 0 : 1, response.Message, "/project/");
        }

        [HttpPost]
        public ActionResult DelProject()
        {
            string pid = Request.QueryString["pid"];
            var response = new ProjectService().DelEntityById(Convert.ToInt32(pid));
            return JsonResultForFly(response.IsSuccess ? 0 : 1, response.Message);
        }

        public ActionResult Detail()
            => View(new ProjectService().GetSingleEntity(EntId));
    
        [HttpPost]
        public ActionResult Detail(ProjectModel model)
        {
            var response = new ProjectService().InsertOrUpdate(model);
            return JsonResultForFly(response.IsSuccess ? 0 : 1, response.Message);
        }

        [HttpPost]
        public ActionResult IsMember(long pid)
        {
            var num = new ProMemberService().IsMember(pid,CurrentUserId);
            return JsonResultForFly(num>0 ? 0 : 1, "非项目成员无法查看");
        }

        public ActionResult Member()
            => View(new ProjectService().GetSingleEntity(EntId));

        public ActionResult MeList()
        {
            var li = new ProMemberService().GetMemberList(Convert.ToInt64(Request["hid"])
                , Convert.ToInt32(Request["page"]), out int count);
            return JsonResultForLay<ProMember, long>(li, count, false);
        }

        public ActionResult Bug()
            => View(new ProjectService().GetSingleEntity(EntId));

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult AddMember(ProMemberModel model)
        {
            var service = new ProMemberService();
            if (model.Role == "1" && service.HasManager()>0)
            {
                return JsonResultForFly(1, "项目经理已经添加了！");
            }
            var response = service.InsertOrUpdate(model);
            return JsonResultForFly(response.IsSuccess ? 0 : 1, response.Message);
        }

        public ActionResult DelMember(long pid,string sno)
        {
            var response = new ProMemberService().DelEntityById(pid, sno);
            return JsonResultForFly(response.IsSuccess ? 0 : 1, response.Message);
        }

        public ActionResult PageList()
        {
            var service = new ProMemberService();
            var member = service.GetDoubleEntity(EntId,CurrentUserId);
            ViewBag.IsManager = member.Role == "1";
            ViewBag.Members= service.GetMembers(EntId);
            return View(service.GetProject(EntId));
        }

        public ActionResult PageDataList()
        {
            var li = new PageInfoService().GetPageList(Convert.ToInt64(Request["hid"])
                , out int count, Convert.ToInt32(Request["page"]));
            return JsonResultForLay<PageInfo, long>(li, count, false);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult AddPage(PageInfoModel model)
        {
            var response = new PageInfoService().InsertOrUpdate(model);
            return JsonResultForFly(response.IsSuccess ? 0 : 1, response.Message);
        }

        [HttpPost]
        public ActionResult DelPage(long pid, string pageId)
        {
            var response = new PageInfoService().DelEntityById(pid, pageId);
            return JsonResultForFly(response.IsSuccess ? 0 : 1, response.Message);
        }

        public ActionResult Pages()
            => View();

        public ActionResult PageInfoList()
           => JsonResultForLay<PageInfo, long>(new PageInfoService().GetPageInfos(Convert.ToInt64(Request["pid"])),0,false);


        public ActionResult Analysis()
            => View(new BugAnalysis() {Id=Convert.ToInt64(Request["id"]),Kind= Request["kind"],Pro =Request["pro"]});

        public ActionResult AnalysisData()
        {
            var kind = Request["kind"];
            var id = Convert.ToInt64(Request["id"]);
            var pro = Request["pro"];
            switch (kind)
            {               
                case "Reviser":
                    return JsonResultForLay(new AnalysisService<AnaReviser>().GetAnalysisData(kind, id,pro));
                case "BookDay":
                    return JsonResultForLay(new AnalysisService<AnaBookDay>().GetAnalysisData(kind, id, pro));
                case "Tester":
                    return JsonResultForLay(new AnalysisService<AnaTester>().GetAnalysisData(kind, id, pro));
                case "BugManager":
                    return JsonResultForLay(new AnalysisService<AnaBugManager>().GetAnalysisData(kind, id, pro));
                case "ReasonSmall":
                    return JsonResultForLay(new AnalysisService<AnaReasonSmall>().GetAnalysisData(kind, id, pro));
                case "ReasonBig":
                    return JsonResultForLay(new AnalysisService<AnaReasonBig>().GetAnalysisData(kind, id, pro));
                case "Scence":
                    return JsonResultForLay(new AnalysisService<AnaScene>().GetAnalysisData(kind, id, pro));
                case "CodeQuality":
                    return JsonResultForLay(new AnalysisService<AnaQuality>().GetAnalysisData(kind, id, pro));
                case "TestQuality":
                    return JsonResultForLay(new AnalysisService<AnaQuality>().GetAnalysisData(kind, id, pro));
                case "SysQuality":
                    return JsonResultForLay(new AnalysisService<AnaQuality>().GetAnalysisData(kind, id, pro));
                default:
                    return JsonResultForLay(new AnalysisService<AnaEveryDay>().GetAnalysisData(kind, id, pro));
            }          
        }


        public ActionResult BugList()
        {
            var li = new BugService().GetBugList(Convert.ToInt64(Request["hid"]), Request["kind"] ?? "IT",
                out int count, Convert.ToInt32(Request["page"]));
            return JsonResultForLay<Bug, long>(li, count, false);
        }

        [HttpPost]
        public ActionResult BugTotol(long pid,string pro)
        {
            var dic = new BugService().GetBugTotol(pid,pro);
            return Json(dic);
        }

        public ActionResult BugInfo()
        {
            var service = new BugService();
            var bug = service.GetDoubleEntity(EntId, EntNo) ?? service.GetEmptyBug(EntId, EntKind);           
            ViewBag.Issuer = bug.Issuer == CurrentUserId;
            ViewBag.IsManager = service.GetRole(EntId,CurrentUserId) == "1";
            return  View(bug);
        }
           
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult BugInfo(BugInfoModel model)
        {
            var response = new BugService().InsertOrUpdate(model);
            return JsonResultForFly(response.IsSuccess ? 0 : 1, response.Message);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult BugRevise(BugReviseModel model)
        {
            var response = new BugService().UpdateBug(model,model.Id,model.No);
            return JsonResultForFly(response.IsSuccess ? 0 : 1, response.Message);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult BugAccept(BugAcceptModel model)
        {
            var response = new BugService().UpdateBug(model, model.Id, model.No);
            return JsonResultForFly(response.IsSuccess ? 0 : 1, response.Message);
        }

        public ActionResult MyProject()
        {
            var li = new ProjectService().GetMyProject(CurrentUserId);
            return JsonResultForFly(0, "", li);
        }


        public  ActionResult Schedule()
            => View(new ProjectService().GetSingleEntity(EntId));

        public ActionResult ScheduleList()
        {
            var li = new BugService().GetBugList(Convert.ToInt64(Request["hid"]), Request["kind"] ?? "IT",
               out int count, Convert.ToInt32(Request["page"]));
            return JsonResultForLay<Bug, long>(li, count, false);
        }

    }
}